package com.hjy.wechatdevelopermode.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;


/**
 * @CopyRight: 版权所有
 * @Author: hejinyi
 * @Description:
 * @Date: 2020/5/21
 */
@Service
public class WeiXinParamesUtil {
    private static final Logger logger = LoggerFactory.getLogger(WeiXinParamesUtil.class);

    public static String tokenUrl;
    public static String wxAppid;
    public static String appsecret;
    /**
     * token
     */
    public static  String token;
    /** encodingAESKey */
    public static  String encodingAESKey;
    /** 菜单创建（POST） 限100（次/天）*/
    public static String menu_create_url;
    /** 删除菜单 */
    public static String delete_menu_create_url;
    /** 查询菜单 */
    public static String query_menu_create_url;
    /** 临时二维码 action_name参数*/
    public  static String QR_SCENE;
    /** 永久二维码 action_name参数*/
    public static  String QR_LIMIT_SCENE;
    /** 永久二维码(字符串) action_name参数*/
    public static  String QR_LIMIT_STR_SCENE;
    /** 创建二维码  */
    public static String create_ticket_path;
    /** 通过ticket换取二维码  */
    public static String showqrcode_path;

    public String getToken() {
        return token;
    }

    @Value("${weChatDeveloperMode.token}")
    public void setToken(String token) {
        WeiXinParamesUtil.token = token;
    }

    public String getEncodingAESKey() {
        return encodingAESKey;
    }

    @Value("${weChatDeveloperMode.encodingAESKey}")
    public void setEncodingAESKey(String encodingAESKey) {
        WeiXinParamesUtil.encodingAESKey = encodingAESKey;
    }

    public String getMenu_create_url() {
        return menu_create_url;
    }

    @Value("${weChatDeveloperMode.menu_create_url}")
    public void setMenu_create_url(String menu_create_url) {
        WeiXinParamesUtil.menu_create_url = menu_create_url;
    }

    public String getDelete_menu_create_url() {
        return delete_menu_create_url;
    }

    @Value("${weChatDeveloperMode.delete_menu_create_url}")
    public void setDelete_menu_create_url(String delete_menu_create_url) {
        WeiXinParamesUtil.delete_menu_create_url = delete_menu_create_url;
    }

    public String getQuery_menu_create_url() {
        return query_menu_create_url;
    }

    @Value("${weChatDeveloperMode.query_menu_create_url}")
    public void setQuery_menu_create_url(String query_menu_create_url) {
        WeiXinParamesUtil.query_menu_create_url = query_menu_create_url;
    }

    public String getQR_SCENE() {
        return QR_SCENE;
    }

    @Value("${weChatDeveloperMode.QR_SCENE}")
    public void setQR_SCENE(String QR_SCENE) {
        WeiXinParamesUtil.QR_SCENE = QR_SCENE;
    }

    public String getQR_LIMIT_SCENE() {
        return QR_LIMIT_SCENE;
    }

    @Value("${weChatDeveloperMode.QR_LIMIT_SCENE}")
    public void setQR_LIMIT_SCENE(String QR_LIMIT_SCENE) {
        WeiXinParamesUtil.QR_LIMIT_SCENE = QR_LIMIT_SCENE;
    }

    public String getQR_LIMIT_STR_SCENE() {
        return QR_LIMIT_STR_SCENE;
    }

    @Value("${weChatDeveloperMode.QR_LIMIT_STR_SCENE}")
    public void setQR_LIMIT_STR_SCENE(String QR_LIMIT_STR_SCENE) {
        WeiXinParamesUtil.QR_LIMIT_STR_SCENE = QR_LIMIT_STR_SCENE;
    }

    public String getCreate_ticket_path() {
        return create_ticket_path;
    }

    @Value("${weChatDeveloperMode.create_ticket_path}")
    public void setCreate_ticket_path(String create_ticket_path) {
        WeiXinParamesUtil.create_ticket_path = create_ticket_path;
    }

    public String getShowqrcode_path() {
        return showqrcode_path;
    }

    @Value("${weChatDeveloperMode.showqrcode_path}")
    public void setShowqrcode_path(String showqrcode_path) {
        WeiXinParamesUtil.showqrcode_path = showqrcode_path;
    }


    public static String getTokenUrl() {
        return tokenUrl;
    }

    @Value("${weChatDeveloperMode.tokenUrl}")
    public static void setTokenUrl(String tokenUrl) {
        WeiXinParamesUtil.tokenUrl = tokenUrl;
    }

    public static String getWxAppid() {
        return wxAppid;
    }

    @Value("${weChatDeveloperMode.wxAppid}")
    public static void setWxAppid(String wxAppid) {
        WeiXinParamesUtil.wxAppid = wxAppid;
    }

    public static String getAppsecret() {
        return appsecret;
    }

    @Value("${weChatDeveloperMode.appsecret}")
    public static void setAppsecret(String appsecret) {
        WeiXinParamesUtil.appsecret = appsecret;
    }

    public static boolean checkSignature(String token, String signature, String timestamp, String nonce) {
        String[] arr = new String[] { token, timestamp, nonce };
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }

    /**
     * 将字节数组转换为十六进制字符
     *
     * @param byteArray
     * @return
     */
    public static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符
     *
     * @param mByte
     * @return
     */
    public static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }




}
